package com.service.user.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.service.user.pojo.UcenterMember;
import com.service.user.service.UcenterMemberService;
import com.service.utils.MD5;
import com.service.utils.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.Get;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-09-30
 */
@RestController
@RequestMapping("user")
@Api(description = "用户管理")
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService ucenterMemberService;

    @GetMapping("queryRegisterCount/{day}")
    @ApiOperation("查询某一个天注册人数")
    public Integer queryRegisterCount(@PathVariable String day){
       return   ucenterMemberService.queryRegisterCount(day);
    }

    @PostMapping("wxSave")
    @ApiOperation("将扫描人的信息添加到数据库中")
    public ResultVo wxSave(@RequestBody Map<String,Object> param){
        UcenterMember ucenterMember = JSONObject.parseObject(JSON.toJSON(param).toString(), UcenterMember.class);
        ucenterMember.setGmtCreate(new Date());
        ucenterMember.setGmtModified(new Date());
        boolean save = ucenterMemberService.save(ucenterMember);
        if (save){
            return ResultVo.ok().data("id",ucenterMember.getId());
        }
        return ResultVo.error();
    }

    @GetMapping("verifyOpenId/{openid}")
    @ApiOperation("验证该微信号是否已注册")
    public ResultVo verifyOpenId(@PathVariable String openid){
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid",openid);
        UcenterMember one = ucenterMemberService.getOne(queryWrapper);
        if (one==null){
            return ResultVo.ok();
        }
        return ResultVo.error().data("id",one.getId());
    }


    @PostMapping("register/{code}")
    @ApiOperation("用户注册")
    public ResultVo register(@ApiParam(name = "ucenterMember",value = "用户注册信息",required = true) @RequestBody UcenterMember ucenterMember,@PathVariable String code){
        return ucenterMemberService.register(ucenterMember,code);
    }


    @GetMapping("verifyMobile/{mobile}")
    @ApiOperation("验证手机号是否可用")
    public ResultVo verifyMobile(@ApiParam(name = "mobile",value = "手机号",required = true)@PathVariable String mobile){
        return ucenterMemberService.verifyMobile(mobile);
    }

    @PostMapping("login")
    @ApiOperation("用户登录")
    public ResultVo login(@ApiParam(name = "mobile",value = "手机号",required = true)   @RequestParam("mobile") String mobile,
                          @ApiParam(name = "password",value = "密码",required = true)        @RequestParam("password") String password){
        return ucenterMemberService.login(mobile,password);
    }


    /**
     * 分页条件查询用户信息
     * @param pageNum
     * @param rows
     * @param ucenterMember
     * @return
     */
    @PostMapping("page")
    @ApiOperation("分页查询所有的用户信息")
    public ResultVo page(
            @ApiParam(name = "pageNum",value = "当前页数")  @RequestParam(defaultValue = "1",value = "pageNum")  Integer pageNum,
            @ApiParam(name = "rows",value = "每页显示条数")  @RequestParam(defaultValue = "5",value = "rows") Integer rows,
            @ApiParam(name = "UcenterMemberQuery",value = "用户查询条件",required = false) @RequestBody UcenterMember ucenterMember
    ){
        return ucenterMemberService.pages(pageNum,rows,ucenterMember);
    }


    /**
     * 查询指定的用户信息
     * @param id
     * @return
     */
    @ApiOperation("查询指定的用户信息")
    @GetMapping("{id}")
    public  ResultVo queryById(@ApiParam(name = "id",value = "指定查询的用户ID",required = true) @PathVariable String id){
        UcenterMember UcenterMember = ucenterMemberService.getById(id);
        return ResultVo.ok().data("item",UcenterMember);
    }

    /**
     * 删除指定的用户信息
     * @param id
     * @return
     */
    @ApiOperation("逻辑删除用户信息")
    @DeleteMapping("{id}")
    public ResultVo delete(@ApiParam(name = "id",value = "逻辑删除的用户ID",required = true) @PathVariable String id){
        boolean b = ucenterMemberService.removeById(id);
        if (b){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 修改用户信息
     * @param ucenterMember
     * @return
     */
    @ApiOperation("修改用户信息")
    @PutMapping
    public ResultVo update(@RequestBody UcenterMember ucenterMember){
        ucenterMember.setGmtModified(new Date());
        boolean b = ucenterMemberService.updateById(ucenterMember);
        if (b){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 添加用户信息
     * @param ucenterMember
     * @return
     */
    @ApiOperation("添加用户信息")
    @PostMapping
    public ResultVo save(@RequestBody UcenterMember ucenterMember){
        ucenterMember.setGmtModified(new Date());
        ucenterMember.setGmtCreate(new Date());
        ucenterMember.setPassword(MD5.encrypt(ucenterMember.getPassword()));
        boolean b = ucenterMemberService.save(ucenterMember);
        if (b){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }


}

